import {MiddlewareSequence} from '@loopback/rest';

// ---------- ADD IMPORTS -------------
export class MySequence extends MiddlewareSequence { }
// export class MySequence implements SequenceHandler {
//   constructor(
//     //     // ---- ADD THIS LINE 需要注意的是这里 ， 官网里面没有下面的好几行代码 ------
//     @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
//     @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
//     @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
//     @inject(SequenceActions.SEND) public send: Send,
//     @inject(SequenceActions.REJECT) public reject: Reject,
//     @inject(AuthenticationBindings.AUTH_ACTION)
//     protected authenticateRequest: AuthenticateFn,
//   ) { }

//   async handle(context: RequestContext) {
//     try {
//       const {request, response} = context;
//       const route = this.findRoute(request);
//       // - enable jwt auth -
//       // call authentication action
//       await this.authenticateRequest(request);

//       const args = await this.parseParams(request, route);
//       const result = await this.invoke(route, args);
//       this.send(response, result);
//     } catch (error) {
//       // - enable jwt auth -
//       // improve the error check
//       if (
//         error.code === AUTHENTICATION_STRATEGY_NOT_FOUND ||
//         error.code === USER_PROFILE_NOT_FOUND
//       ) {
//         Object.assign(error, {statusCode: 401 /* Unauthorized */});
//       }
//       this.reject(context, error);
//     }
//   }
// }

// 下方为for token使用
// export class MyHandler implements SequenceHandler {
//   constructor(
//     // ---- ADD THIS LINE 需要注意的是这里 ， 官网里面没有下面的好几行代码 ------
//     @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
//     @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
//     @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
//     @inject(SequenceActions.SEND) public send: Send,
//     @inject(SequenceActions.REJECT) public reject: Reject,
//     @inject(AuthenticationBindings.AUTH_ACTION)
//     protected authenticateRequest: AuthenticateFn,
//   ) { }
//   async handle(context: RequestContext) {
//     try {
//       const {request, response} = context;
//       const route = this.findRoute(request);
//       // - enable jwt auth -
//       // call authentication action
//       // ---------- ADD THIS LINE -------------
//       await this.authenticateRequest(request);
//       const args = await this.parseParams(request, route);
//       const result = await this.invoke(route, args);
//       this.send(response, result);
//     } catch (err) {
//       // ---------- ADD THIS SNIPPET -------------
//       // if error is coming from the JWT authentication extension
//       // make the statusCode 401
//       if (
//         err.code === AUTHENTICATION_STRATEGY_NOT_FOUND ||
//         err.code === USER_PROFILE_NOT_FOUND
//       ) {
//         Object.assign(err, {statusCode: 401 /* Unauthorized */});
//       }
//       // ---------- END OF SNIPPET -------------
//       this.reject(context, err);
//     }
//   }
// }

